package com.zhaoyun.test;

import android.content.Context;
import android.os.SystemClock;


/**
 * Created by suche on 2017/11/3.
 */

public class ChargeSerialHelper extends J2XXSerialHelper {


    public ChargeSerialHelper(Context context, int index) {
        super(context,index);
    }

    @Override
    protected void onDataReceived(ComBean ComRecData) {
        MyLog.d("tag", "ComRecData -------> " + ComRecData.toString());
    }



    @Override
    protected void openComPort() {
        openSerialPort();
    }

    @Override
    protected void closeComPort() {
        closeSerialPort();
    }

    protected void opneChargeSeial(int index){
        byte[] cmd = new byte[]{(byte) 0x98, (byte) 0x68, (byte) 0xff, (byte) 0x01, (byte) 0x55, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x01, (byte) 0xFE, (byte) 0xC2, (byte) 0x3B};
        byte b = (byte) index;
        cmd[7] = b;
        cmd[10] = 0x00; //(byte) (crc / 256);
        cmd[11] = 0x00; //(byte) (crc % 256);
        comSend(cmd);
    }

    protected void closeChargeSerial(int index){
        byte[] cmd = new byte[]{(byte) 0x98, (byte) 0x68, (byte) 0xff, (byte) 0x01, (byte) 0x55, (byte) 0x02, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0xFE, (byte) 0xC3, (byte) 0xAB};
        byte b = (byte) index;
        cmd[7] = b;
       // int crc = CRC16Util.calcCrc16(new byte[]{(byte) 0x98, (byte) 0x68, (byte) 0xff, (byte) 0x01, (byte) 0x55, (byte) 0x02, (byte) 0x02, b, (byte) 0x00, (byte) 0xFE});
        cmd[10] = 0x00;//(byte) (crc / 256);
        cmd[11] = 0x00; //(byte) (crc % 256);
        comSend(cmd);
    }

    private synchronized void comSend(byte[] cmd) {
        sendCmd(cmd.length, cmd);
//        MyLog.d("tag", "send --> " + MyFunc.ByteArrToHex(cmd));
        SystemClock.sleep(500);
    }
}
